/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entidades;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "COMPRA")
@NamedQueries({
    @NamedQuery(name = "Compra.findAll", query = "SELECT c FROM Compra c"),
    @NamedQuery(name = "Compra.findByCodigo", query = "SELECT c FROM Compra c WHERE c.codigo = :codigo"),
    @NamedQuery(name = "Compra.findByDescripcion", query = "SELECT c FROM Compra c WHERE c.descripcion = :descripcion"),
    @NamedQuery(name = "Compra.findByNroFactura", query = "SELECT c FROM Compra c WHERE c.nroFactura = :nroFactura"),
    @NamedQuery(name = "Compra.findByFecha", query = "SELECT c FROM Compra c WHERE c.fecha = :fecha"),
    @NamedQuery(name = "Compra.findByImporte", query = "SELECT c FROM Compra c WHERE c.importe = :importe"),
    @NamedQuery(name = "Compra.findByExtra", query = "SELECT c FROM Compra c WHERE c.extra = :extra"),

    @NamedQuery(name = "Compra.findTotalGastosMes", query = "SELECT sum(c.importe) + sum(c.extra) FROM Compra c WHERE MONTH(c.fecha) = :mes AND YEAR(c.fecha) = :anio GROUP BY YEAR(c.fecha), MONTH(c.fecha)"),
    @NamedQuery(name = "Compra.findTotalGastosAnio", query = "SELECT sum(c.importe) + sum(c.extra) FROM Compra c WHERE YEAR(c.fecha) = :anio GROUP BY YEAR(c.fecha)"),

    @NamedQuery(name = "Compra.findGastosMes", query = "SELECT MONTH(c.fecha), sum(c.importe) + sum(c.extra) FROM Compra c WHERE YEAR(c.fecha) = :anio GROUP BY YEAR(c.fecha), MONTH(c.fecha)")
})

public class Compra implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "CODIGO")
    private Integer codigo;
    @Column(name = "DESCRIPCION")
    private String descripcion;
    @Basic(optional = false)
    @Column(name = "NRO_FACTURA")
    private String nroFactura;
    @Column(name = "FECHA")
    @Temporal(TemporalType.DATE)
    private Date fecha;
    @Column(name = "IMPORTE")
    private Double importe;
    @Column(name = "EXTRA")
    private Double extra;
    @JoinColumn(name = "CODIGO_CAT", referencedColumnName = "CODIGO")
    @ManyToOne
    private Categoria categoria;
    @JoinColumn(name = "ID_PROV", referencedColumnName = "CODIGO")
    @ManyToOne
    private Proveedor proveedor;

    public Compra() {
    }

    public Compra(Integer codigo) {
        this.codigo = codigo;
    }

    public Compra(Integer codigo, String nroFactura) {
        this.codigo = codigo;
        this.nroFactura = nroFactura;
    }

    public Integer getCodigo() {
        return codigo;
    }

    public void setCodigo(Integer codigo) {
        this.codigo = codigo;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getNroFactura() {
        return nroFactura;
    }

    public void setNroFactura(String nroFactura) {
        this.nroFactura = nroFactura;
    }

    public Date getFecha() {
        return fecha;
    }

    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }

    public Double getImporte() {
        return importe;
    }

    public void setImporte(Double importe) {
        this.importe = importe;
    }

    public Double getExtra() {
        return extra;
    }

    public void setExtra(Double extra) {
        this.extra = extra;
    }

    public Categoria getCategoria() {
        return categoria;
    }

    public void setCategoria(Categoria categoria) {
        this.categoria = categoria;
    }

    public Proveedor getProveedor() {
        return proveedor;
    }

    public void setProveedor(Proveedor proveedor) {
        this.proveedor = proveedor;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (codigo != null ? codigo.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Compra)) {
            return false;
        }
        Compra other = (Compra) object;
        if ((this.codigo == null && other.codigo != null) || (this.codigo != null && !this.codigo.equals(other.codigo))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entidades.Compra[codigo=" + codigo + "]";
    }

}
